package com.tanhua.common.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.UserFreezeMapper;
import com.tanhua.common.pojo.UserFreeze;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @author Ling
 * @Title: null.java
 * @Package com.tanhua.manage.service
 * @Description:
 * @date 2021/9/13 11:15
 */
@Service
@Slf4j
public class UserFreezeService {
    @Autowired
    private UserFreezeMapper userFreezeMapper;
    @Autowired
    private JournalApiService journalApiService;

    /**
     * 冻结用户
     *

     * @return
     */
    public Boolean freezeUser(UserFreeze freeze) {
        if (isFrozen(freeze.getUserId())) {
            return false;
        }
        UserFreeze userFreeze = new UserFreeze();
        userFreeze.setUserId(freeze.getUserId());
        userFreeze.setFreezeTime(new Date());
        userFreeze.setFreezingTime(getFreezeLast(freeze.getFreezingTime()));
        userFreeze.setReasonsForFreezing(freeze.getReasonsForFreezing());
        userFreeze.setFrozenRemarks(freeze.getFrozenRemarks());
        userFreeze.setFreezingRange(freeze.getFreezingRange());
        int i = userFreezeMapper.insert(userFreeze);
        journalApiService.saveLog("冻结用户", "冻结(" + freeze.getUserId() + ")用户" + userFreeze.getFreezingTime() + "天，冻结原因：" + freeze.getReasonsForFreezing() + "备注" + freeze.getFrozenRemarks() + "。");
        return i > 0;
    }


    /**
     * 获取持续时间 因与数据库定义不符 需进行处理 默认冻结时间为3天
     *
     * @param freezingTime
     * @return
     */
    private Integer getFreezeLast(Integer freezingTime) {
        if (freezingTime == 1) {
            return 3;
        } else if (freezingTime == 2) {
            return 7;
        } else if (freezingTime == 3) {
            return -1;
        }
        return 3;
    }

    /**
     * 解冻用户
     *
     * @param userId            用户id
     * @param reasonsForThawing 解冻原因
     * @return
     */
    public Boolean unfreezeUser(Long userId, String reasonsForThawing) {
        if (!isFrozen(userId)) {
            return false;
        }
        UserFreeze userFreeze = getUserFreeze(userId);
        userFreeze.setFreezingRange(0);
        userFreeze.setFreezingTime(0);
        userFreeze.setFreezeEndTime(new Date());
        userFreeze.setUnfreezeComment(reasonsForThawing);
        int i = userFreezeMapper.updateById(userFreeze);
        journalApiService.saveLog("解除冻结用户", "解除冻结(" + userId + ")用户，冻结原因：" + reasonsForThawing + "。");
        return i > 0;
    }

    /**
     * 判断用户是否被冻结
     *
     * @param userId
     * @return
     */
    public Boolean isFrozen(Long userId) {

        UserFreeze userFreeze = getUserFreeze(userId);
        if (userFreeze == null) {
            return false;
        }
        Long freezeTime = userFreeze.getFreezeTime().getTime();
        Integer freezeLast = userFreeze.getFreezingTime();
        if (freezeLast == -1) {
            return true;
        }
        return isFrozen(freezeTime, freezeLast);
    }

    /**
     * 根据冻结时间与冻结持续天数 对比今天的毫秒数 获取是否被冻结
     *
     * @param freezeTime
     * @param freezeLast
     * @return
     */
    private boolean isFrozen(Long freezeTime, Integer freezeLast) {
        return System.currentTimeMillis() < ((freezeLast * 24 * 60 * 60 * 1000) + freezeTime);
    }

    /**
     * 获取被冻结的用户
     *
     * @param userId
     * @return
     */
    private UserFreeze getUserFreeze(Long userId) {
        QueryWrapper<UserFreeze> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.ne("freeze_type", 0);
        wrapper.orderByDesc("freeze_time");
        wrapper.last("limit 1");
        return userFreezeMapper.selectOne(wrapper);
    }

    //todo 取得是什么
    public List<UserFreeze> getUserFreezeByUserIdList(Collection<?> userIdList) {
        QueryWrapper<UserFreeze> wrapper = new QueryWrapper<>();
        wrapper.in("user_id", userIdList);
        wrapper.ne("freeze_type", 0);
//        wrapper.gt("freeze_time", (System.currentTimeMillis() - (3 * 24 * 60 * 60 * 1000)));
        wrapper.orderByDesc("freeze_time");
        List<UserFreeze> userFreezes = userFreezeMapper.selectList(wrapper);
        List<UserFreeze> result = new ArrayList<>();
        for (UserFreeze userFreeze : userFreezes) {
            if (isFrozen(userFreeze.getFreezeTime().getTime(), userFreeze.getFreezingTime())) {
                result.add(userFreeze);
            }
        }
        return result;
    }
}
